function Look(){}

var look = new Look();

Look.prototype.setTitle = function(title){
	document.title = title;
	_gaq.push(['_trackEvent', 'click', 'page', title]);
}

/**
 * 提示信息
 */
Look.prototype.showInfo = {
	showTimer : $.timer(function() {
		look.showInfo.hide();
	}),
	add : function(){
		if($(".info_panel").length==0){
			$("body").prepend('<div class="info_panel" id="infoPanel"><span></span></div>');
		}
	},
	hide : function(){
		this.showTimer.stop();
		this.add();
		$("#infoPanel").fadeOut();
	},
	right : function(text){
		this.add();
		this.showTimer.stop();
		this.showTimer.once(5000);
		$("#infoPanel").fadeIn().children("span").removeClass("errorInfo").html(text);
	},
	error : function(text){
		this.right(text);
		$("#infoPanel span").addClass("errorInfo");
	}
}

/**
 * 邮箱验证
 * @param str
 * @return
 */
Look.prototype.verifyEmail = function(str){
	var emailReg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	return !emailReg.test(str);
}

/**
 * 生成id
 * @return
 */
Look.prototype.generateGuid = function(){
    var guid = "";
    for (var i = 1; i <= 32; i++){
          var n = Math.floor(Math.random()*16.0).toString(16);
          guid += n;
//        if((i==8)||(i==12)||(i==16)||(i==20))
//        	guid += "-";
    }
    return guid;
}

/**
 * 重新设置参数值
 * @param {Array} key 如：[["aa","11"],["bb","22"]]
 * @param {String} source
 * @return
 */
Look.prototype.setParam = function(key, source){
	var theNewStr = source;
	function single(keyStr, sourceStr){
		var arr = sourceStr.split("&"),
			newArr = "";
		for (var i = 0; i < arr.length; i++){
			if (arr[i].split("=")[0] == keyStr[0]) {
				newArr += keyStr[0] + "=" + keyStr[1] + "&";
			}else{
				newArr += arr[i].split("=")[0] + "=" + arr[i].split("=")[1] + "&";
			}
		}
		return newArr.replace(/[&]$/, "");
	}
	for(var j = 0; j < key.length; j++){
		theNewStr = single(key[j], theNewStr);
	}
	return theNewStr.replace(/[&]$/, "");
}

/**
 * 获取指定的URL中的hash参数值 
 * @param {String} paramName URL参数
 * @return paramValue URL参数值
 */
Look.prototype.getHashParam = function(paramName) {
	var paramValue = "";
	var isFound = false;
	if (location.hash.indexOf("#") == 0 && location.hash.indexOf("=") > 1) {
		var arrSource = unescape(location.hash).substring(1,location.hash.length).split("&");
		i = 0;
		while (i < arrSource.length && !isFound) {
			if (arrSource[i].indexOf("=") > 0) {
				if (arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase()) {
					paramValue = arrSource[i].split("=")[1];
					isFound = true;
				}
			}
			i++;
		}
	}
	return paramValue;
}

/**
 * 获取String中的参数值
 * @param key
 * @param source
 * @return
 */
Look.prototype.getStringParam = function(key,source){
	if (source.indexOf("#") != -1 || source.indexOf("?") != -1) {
		source = source.substr(1);
	}
	if(source){
		var arr = source.split("&");
		for (var i=0; i<arr.length; i++){
			if(arr[i].split("=")[0] == key){
				return arr[i].split("=")[1];
			}
		}
	}
}

/**
 * 返回字符串在规定字节长度内的值  
 * @param val
 * @param max
 * @return String
 */
Look.prototype.getByteVal = function(val,max) {   
	var returnValue='';
	var byteValLen=0;
	var val=val.split("");
	for (var i=0;i<val.length;i++){
		if(val[i].match(/[^\x00-\xff]/ig)!=null){
			byteValLen+=2;
		}else {
			byteValLen+=1;
		}
		if (byteValLen>max){
			break;
		}
		returnValue+=val[i];
	}
	return returnValue;
}

/**
 * 关闭窗口
 */
function closeWindow(){
	//获取浏览器的头部信息
	var ua=navigator.userAgent;
	var ie=navigator.appName=="Microsoft Internet Explorer"?true:false;//是否ie浏
	if(ie){
		var IEversion=parseFloat(ua.substring(ua.indexOf("MSIE")+5,ua.indexOf(";",ua.indexOf("MSIE "))));
		if(IEversion<5.5){
			var str='<object id=noTipClose classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">';
			str+='<param name="Command" value="Close">';
			//只有ie支持
			document.body.insertAdjacentHTML("beforeEnd",str);
			document.all.noTipClose.click();
		}else{
			window.opener=null;
			window.close();
		}
	}else{
		parent.window.opener = null;
		parent.window.open("", "_self");
		parent.window.close();
	}
}

/**
 * 请求计数，放在cookie中
 * @time 保存时间
 * @count 次数后阻止
 */
Look.prototype.requestCount = {
	time : 10,
	count : 3,
	nick : function(){
		return $.cookie("user_name") ? $.cookie("user_name") : "尊敬的用户";
	},
	init : function(key){
		var $time = this.time,
			$count = this.count;
		if(!$.cookie(key)){
			$.cookie(key, 0, {expires : $time/24/60/60});
		}
		if($.cookie(key) > $count){
			look.showInfo.error(this.nick() + '</b>，您的操作过于频繁，请休息一会儿吧。。');
			return false;
		}else{
			$.cookie(key, parseInt($.cookie(key)) + 1, {expires : $time/24/60/60});
			return true;
		}
	}
}

/**
 * 数据请求方法
 * @param requestData
 * @param httpHeader
 * @param responseFn
 * @return
 */
Look.prototype.dataConn = function(url, requestData, responseFn, completeFn){
	$.ajax({
		type : "POST",
		url : url,
		dataType : "text",
		data : requestData,
		timeout : 15000,
		success : function(data, textStatus, XMLHttpRequest){
			var back = eval('(' + (data ? data : null) + ')');
			if(back != null){
				if(back.code != 0){
					look.showInfo.error(back.code + "：" + back.message);
					return false;
				}
				if(responseFn != undefined){
					try {
						eval(responseFn(back, textStatus));
					} catch (e) {
						look.showInfo.error("[ERROR] name: " + e.name + "message: " + e.message);
					}
				}
			}else{
				look.showInfo.error("网络连接出错！");
			}
		},
		error : function(XMLHttpRequest, error){
			if(error == "timeout"){
				look.showInfo.error("请求超时：请求系统返回数据超时！请稍候再试吧…");
			}else{
				look.showInfo.error(error + "：返回数据可能为非JSON格式…");
			}
		},
		complete : function(XMLHttpRequest, textStatus){
			if(completeFn != undefined){
				try {
					eval(completeFn(textStatus));
				} catch (e) {
					look.showInfo.error("[ERROR] name: " + e.name + "message: " + e.message);
				}
			}
		}
	});
}

/**
 * 同步加载页面
 * @param {String} container 页面存放的div容器
 * @param {String} url 加载的页面URL
 * @param {String} callback 回调函数
 */
Look.prototype.loadPage = function(container,url,callback) {
	$.ajax({
		url : url,
		async : false,
		success : function(html) {
			$(container).html(html);
		},
		complete : function() {
			if(callback!=undefined){
				eval(callback);
			}
		},
		error : function(XmlHttpRequest, textStatus, errorThrown) {
			look.showInfo.error("加载页面出错，请进行刷新，然后再试一次。");
		}
	});
}

/**
 * 分页
 * @param id 插入页码的地方
 * @param pageSize 每页数目
 * @param hashArray hash数组值
 * @param count 总数
 * @return
 */
Look.prototype.setPager = function(id,pageSize,hashArray,count){
	var theHtml="",className="";
	if(count==0 || count==undefined){
		$("#goodsPager").html(theHtml);
		return false;
	}
	var currentPager=1,pageStep=5,pageCount,beginPage,endPage;
	//分页算法
	pageCount=Math.ceil(count/pageSize);
	currentPager=parseInt(hashArray[1] ? hashArray[1] : 1);
	if(pageCount<currentPager){currentPager=pageCount;}
	if(currentPager<1){currentPager=1;}
	beginPage=(currentPager-Math.ceil(pageStep/2));
	if(beginPage<1){beginPage=1;}
	endPage=currentPager+pageStep/2;
	if(endPage>pageCount){endPage=pageCount+1;}
	
	if(currentPager==1){
		theHtml+='<a href="javascript:" class="disabled">首页</a>';
		theHtml+='<a href="javascript:" class="disabled">上一页</a>';
	}else{
		theHtml+='<a href="'+hashArray[0]+'">首页</a>';
		if(currentPager==2){
			theHtml+='<a href="'+hashArray[0]+'">上一页</a>';
		}else{
			theHtml+='<a href="'+hashArray[0]+"&page="+(currentPager-1)+'">上一页</a>';
		}
	}
	for(var i=beginPage;i<endPage;i++){
		if(currentPager==i){
			className="selected";
		}else{
			className="";
		}
		if(i==1){
			theHtml+='<a href="'+hashArray[0]+'" class="'+className+'">'+(i)+'</a>';
		}else{
			theHtml+='<a href="'+hashArray[0]+"&page="+i+'" class="'+className+'">'+(i)+'</a>';
		}
	}
	if(currentPager==pageCount){
		theHtml+='<a href="javascript:" class="disabled">下一页</a>';
		theHtml+='<a href="javascript:" class="disabled">末页</a>';
	}else{
		theHtml+='<a href="'+hashArray[0]+"&page="+(currentPager+1)+'">下一页</a>';
		theHtml+='<a href="'+hashArray[0]+"&page="+pageCount+'">末页</a>';
	}
	$(id).html(theHtml);
}
